//分区
var Fenqu = RichBase.extend({
	EVENTS : {
		"click" : {
			".addBtn" : "addFenqu",
			".triggerColorBtn" : "switchColor",
			".minuBtn" : "minuFenqu"
		},
		"blur" : {
			".formInp" : "onInputBlur"
		},
		"focus" : {
			".formInp" : "onInputFocus"
		}
	},
	statics : {
		getUUID : (function(){
			var id = $("#fenUl").children().last().attr("data-key");
			id = Number(id);
			return function(){
				id++
				return id;
			}
		})()
	},
	init : function(opt){
		this.container = opt.container;
		this.listUl = $("#fenUl");
	},
	addFenqu : function(that,e){
		var tpl = $("#fenqu-list-tpl").html();
		var areaid = that.statics.getUUID();
		that.listUl.append(that.parseTemplate(tpl,{areaid:areaid}));
	},
	minuFenqu : function(that,e){
		var tarBtn = $(e.currentTarget);
		var parent = tarBtn.parent();
		var areaidInp = parent.children(".areaidHidInp");
		if(areaidInp.val()!=0){ //编辑已存在的分区时，不要真的删除dom
			parent.hide().find(".isDelectHidInp").val(0);
		}else{
			parent.remove();
		}
	},
	switchColor : function(that,e){
		var triggerBtn = $(e.currentTarget);
		var color = triggerBtn.attr("data-color");
		if(!that.colorDialog) that.colorDialog = that.initDialog();
		that.colorDialog.open({
			speed : 300,
			offset : -30,
			onAfterOpen : function(dialog){
				dialog.hocks.triggerBtn = triggerBtn;
			}
		})
	},
	initDialog : function(){
		var self = this;
		return new PFT.Dialog({
			title : "选择分区颜色",
			content : self.buildColorList(),
			EVENTS : {
				"click" : {
					".colorUl .colorBox" : function(that,e){
						var color = $(e.currentTarget).attr("data-color");
						that.hocks.triggerBtn.css("backgroundColor","#"+color).val(color);
						that.close({speed:200});
					}
				}
			}
		})
	},
	onInputFocus : function(that,e){
		var tarInp = $(e.currentTarget);
		tarInp.removeClass("error").siblings(".errorTip").hide();
	},
	onInputBlur : function(that,e){
		var tarInp = $(e.currentTarget);
		var errorTip = tarInp.siblings(".errorTip");
		if(!tarInp.val()){
			tarInp.addClass("error");
			errorTip.show();
		}
	},
	buildColorList : function(){
		var html = "";
		html += '<ul class="colorUl">';
		for(var i in COLORS){
			var color = COLORS[i];
			html += '<li class="colorBox" data-color="'+color+'" style="background-color:#'+color+'"></li>';
		}
		html += '</ul>';
		return html;
	}
});

var Venues = RichBase.extend({
	api : "call/jh_show.php",
	EVENTS : {
		"blur" : {
			"#venue_name_inp" : "onVenueNameInpBlur"
		},
		"focus" : {
			"#venue_name_inp" : "onVenueNameInpFocus"
		},
		"click" : {
			"#submitBtn" : "onSubmit"
		}
	},
	init : function(opt){
		this.container = opt.container;
		//分区
		opt.Fenqu && new opt.Fenqu({container:$("#submitForm")});
		var venuenameHead = $("#venuenameHead");
		if(venuenameHead.attr("data-venueid")){
			venuenameHead.find(".edit").show().siblings().hide();
		}else{
			venuenameHead.find(".create").show().siblings().hide();
		}
	},
	//提交保存
	onSubmit : function(that,e){
		e.preventDefault();
		var submitBtn = $(e.currentTarget);
		var allow = true;
		if(submitBtn.hasClass("loading")) return false;
		that.container.find(".formInp").each(function(){
			var tarInp = $(this);
			tarInp.blur();
			if(tarInp.hasClass("error")){
				allow = false;
				return false;
			}
		});
		if(!allow) return false;
		if(!$("#venuse_thumb_inp").val()){
			$("#errorTip_thum").show();
			return false;
		}
		//验证通过开始上传
		PFT.Ajax({
			url : that.api,
			type : "post",
			dataType : "json",
			data : $("#submitForm").serialize(),
			loading : function(){ submitBtn.text("正在保存...").addClass("loading")},
			removeLoading : function(){ submitBtn.text("下一步").removeClass("loading")},
			timeout : function(){ alert("保存数据超时，请稍后重试") },
			serverError : function(){ alert("保存数据出错，请稍后重试") }
		},function(res){
			if(res.status=="success"){
				PFT_GLOBAL.U.Alert('success','<p style="width:120px">保存成功</p>');
				window.location.href = "pft_show_seat.html?m=venue&venue_id="+res.venue_id;
//				window.location.href = "pft_show.html?m=seat&venue_id="+res.venue_id;
			}else{
				var mesg = res.msg || "保存失败";
				PFT_GLOBAL.U.Alert('fail','<p style="width:180px">'+mesg+'</p>');
			}
		})
	},
	onVenueNameInpBlur : function(that,e){
		var errorTip = $("#errorTip_venues");
		var tarInp = $(e.currentTarget);
		var venuesHidInp = $("#venue_name_hidInp");
		var val = tarInp.val();
		if(!val){
			tarInp.addClass("error");
			errorTip.show();
		}
		venuesHidInp.val(val);
	},
	onVenueNameInpFocus : function(that,e){
		var errorTip = $("#errorTip_venues");
		var tarInp = $(e.currentTarget);
		tarInp.removeClass("error");
		errorTip.hide();
	}
});


//图片上传
var uploador = new ImgUploador({
	container : $("#mapUpload-container")
});
uploador.on("oncomplete",function(res){
	$("#venuse_thumb_inp").val(res.src);
});

$(function(){
	new Venues({
		container : $("#guanContainer"),
		Fenqu : Fenqu
	});
})


